package com.ihome.android.util;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ihome.android.academy.R;
import com.ihome.android.market2.frag.SearchAppFragment;
import com.ihome.android.util.AppConsts;
import com.ihome.android.util.AppConsts.KeyDirection;

public class LetterKey extends KeyFunction {
	public static View			clickView		= null;
	private static View			kb				= null;
	public static LinearLayout	lpan			= null;
	private final int			MSG_SHOW_BOARD	= 0;
	private Handler				handler			= new Handler() {
													@Override
													public void handleMessage(Message msg) {
														if (msg.what == MSG_SHOW_BOARD) {
															sendUsrInput((View) msg.obj, true);
														}
													}
												};

	public LetterKey(Context paramContext, KeyItem paramKeyItem, View parent) {
		super(paramContext);
		this.item = paramKeyItem;
		lpan = (LinearLayout) parent.findViewById(R.id.lpan);
	}

	private void bindKeysEvent(SingleKeyActionItem paramSingleKeyActionItem) {
		CustomKeyboard.setKeyBoards(false);
		TextView localTextView1 = (TextView) kb.findViewById(R.id.tvUp);
		TextView localTextView2 = (TextView) kb.findViewById(R.id.tvDown);
		TextView localTextView3 = (TextView) kb.findViewById(R.id.tvLeft);
		TextView localTextView4 = (TextView) kb.findViewById(R.id.tvRight);
		TextView localTextView5 = (TextView) kb.findViewById(R.id.tvCenter);
		localTextView1.setText(paramSingleKeyActionItem.getUpText());
		localTextView2.setText(paramSingleKeyActionItem.getDownText());
		localTextView3.setText(paramSingleKeyActionItem.getLeftText());
		localTextView4.setText(paramSingleKeyActionItem.getRightText());
		localTextView5.setText(paramSingleKeyActionItem.getCenterText());
		localTextView1.setOnFocusChangeListener(new FocusChangeListener(kb, AppConsts.KeyDirection.Up, localTextView1));
		localTextView3.setOnFocusChangeListener(new FocusChangeListener(kb, AppConsts.KeyDirection.Left, localTextView3));
		localTextView4.setOnFocusChangeListener(new FocusChangeListener(kb, AppConsts.KeyDirection.Right, localTextView4));
		localTextView5.setOnFocusChangeListener(new FocusChangeListener(kb, AppConsts.KeyDirection.Center, localTextView5));
		localTextView1.setOnKeyListener(kListener);
		localTextView2.setOnKeyListener(kListener);
		localTextView3.setOnKeyListener(kListener);
		localTextView4.setOnKeyListener(kListener);
		localTextView5.setOnKeyListener(kListener);
		if ((paramSingleKeyActionItem.getDownText() != null) && (!paramSingleKeyActionItem.getDownText().equals(""))) {
			localTextView2.setFocusable(true);
			localTextView2.setFocusableInTouchMode(true);
			localTextView2.setOnFocusChangeListener(new FocusChangeListener(kb, AppConsts.KeyDirection.Down, localTextView2));
		}
		else {
			localTextView2.setFocusable(false);
			localTextView2.setFocusableInTouchMode(false);
		}

		localTextView5.requestFocus();
	}

	private void loadKeysAndPosition(View paramView) {
		lpan.setVisibility(View.VISIBLE);
		if ((lpan != null) && (lpan.getChildCount() == 0)) {
			kb = LayoutInflater.from(this.context).inflate(R.layout.key_action, null);
			LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			kb.setLayoutParams(localLayoutParams);
			lpan.addView(kb);
		}

		int[] arrayOfInt = new int[2];
		paramView.getLocationOnScreen(arrayOfInt);
		int i, j;

		if (SearchAppFragment.firstEnter == true) {
			i = arrayOfInt[0] - paramView.getWidth() / 2 - 48;
			SearchAppFragment.firstEnter = false;
		}
		else {
			i = arrayOfInt[0] - paramView.getWidth() / 2 + 5;
		}
		j = arrayOfInt[1] - paramView.getHeight() / 2 - 110;

		lpan.setX(i);
		lpan.setY(j);
	}

	private void refreshKeyState(View paramView1, View paramView2) {
		GridLayout localGridLayout = (GridLayout) paramView1.findViewById(R.id.glLetter);
		for (int i = 0;; i++) {
			if (i >= localGridLayout.getChildCount()) {
				((TextView) paramView2).setTextColor(this.context.getResources().getColor(R.color.key_sel));
				return;
			}
			((TextView) localGridLayout.getChildAt(i)).setTextColor(Color.WHITE);
		}
	}

	public AppConsts.KeyType getKeyType() {
		return AppConsts.KeyType.Letter;
	}

	public View.OnClickListener getOnClickListener() {
		return new View.OnClickListener() {
			public void onClick(View paramAnonymousView) {
				CustomKeyboard.sendMsg(false);
				SingleKeyActionItem localSingleKeyActionItem = ((SingleKeyItem) paramAnonymousView.getTag()).getActionKeys();
				LetterKey.clickView = paramAnonymousView;
				LetterKey.this.loadKeysAndPosition(paramAnonymousView);
				LetterKey.this.bindKeysEvent(localSingleKeyActionItem);
			}
		};
	}

	private class ClickListener implements View.OnClickListener {
		private ClickListener() {}

		public void onClick(View paramView) {
			TextView localTextView = (TextView) paramView;
			CustomKeyboard.sendCommand(LetterKey.this.context, AppConsts.SearchType.Data, localTextView.getText().toString(), item.id);
		}
	}

	public class FocusChangeListener implements View.OnFocusChangeListener {
		View					kb			= null;
		AppConsts.KeyDirection	kd			= null;
		View					mContentTX	= null;

		public FocusChangeListener(View paramView, AppConsts.KeyDirection paramKeyDirection, View contentTX) {
			kb = paramView;
			kd = paramKeyDirection;
			mContentTX = contentTX;
		}

		public void onFocusChange(View paramView, boolean paramBoolean) {
			if (paramBoolean) {
				switch (kd) {
				case Up:
				case Down:
				case Left:
				case Right:

					if (kd == KeyDirection.Up) {
						kb.setBackgroundResource(R.drawable.key_up_bg);
					}
					else if (kd == KeyDirection.Down) {
						kb.setBackgroundResource(R.drawable.key_down_bg);
					}
					else if (kd == KeyDirection.Left) {
						kb.setBackgroundResource(R.drawable.key_left_bg);
					}
					else if (kd == KeyDirection.Right) {
						kb.setBackgroundResource(R.drawable.key_right_bg);
					}
					else {}

					Message msg = new Message();
					msg.obj = paramView;
					msg.what = MSG_SHOW_BOARD;
					handler.sendMessageDelayed(msg, 100);
					break;
				case Center:
					kb.setBackgroundResource(R.drawable.key_center_bg);
					break;
				default:
					break;
				}
				LetterKey.this.refreshKeyState(kb, paramView);
			}
		}
	}

	View.OnKeyListener	kListener	= new OnKeyListener() {

										@Override
										public boolean onKey(View v, int keyCode, KeyEvent event) {
											if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
												sendUsrInput(v, true);
												return true;
											}
											// TODO Auto-generated method stub
											if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
												CustomKeyboard.setKeyBoards(true);
												LetterKey.lpan.setVisibility(View.GONE);
												LetterKey.clickView.requestFocus();
												CustomKeyboard.sendMsg(true);
												return true;
											}
											return false;
										}
									};

	private void sendUsrInput(View paramView, Boolean closed) {
		TextView localTextView = (TextView) paramView;
		CustomKeyboard.sendCommand(LetterKey.this.context, AppConsts.SearchType.Data, localTextView.getText().toString(), item.id);
		CustomKeyboard.sendMsg(true);
		if (closed == true) {
			CustomKeyboard.setKeyBoards(true);
			LetterKey.lpan.setVisibility(View.GONE);
			LetterKey.clickView.requestFocus();
		}

	}
}
